using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace ReportMaintenanceUtility
{
	public class DefaultValueList : CustomList<DefaultValue>
	{
		#region CONSTRUCTOR(S)
		public DefaultValueList() : base() { }
		public DefaultValueList(ValuesType vt)
		{
			if (vt == null)
			{
				throw new ArgumentNullException();
			}
			else
			{
				this._initializing = true;
				this._seed = vt;
				if (vt.Value != null)
				{
					for (int i = 0; i < vt.Value.Length; i++)
					{
						this.Add(new DefaultValue(vt, i));
					}
				}
				this._initializing = false;
			}
		}
		protected DefaultValueList(DefaultValueList list) : base(list) { }
		#endregion

		#region OVERRIDES
		protected override void InsertItem(int index, DefaultValue item)
		{
			if (!this._initializing)
			{
				this.PrepStateChange("DefaultValueListItem");

				ValuesType vts = (ValuesType)this._seed;
				List<string> vtList = new List<string>(vts.Value);

				if (!vtList.Contains(item.Value))
				{
					vtList.Add(item.Value);
					vts.Value = vtList.ToArray();
				}
			}
			base.InsertItem(index, item);
		}
		protected override void RemoveItem(int index)
		{
			if (!this._initializing)
			{
				this.PrepStateChange("DefaultValueListItem");

				ValuesType vts = (ValuesType)this._seed;
				List<string> vtList = new List<string>(vts.Value);
				vtList.Remove(this[index].Value);
				vts.Value = vtList.ToArray();
			}
			base.RemoveItem(index);
		}
		protected override void SetItem(int index, DefaultValue item)
		{
			if (!this._initializing)
			{
				this.PrepStateChange("DefaultValueListItem");

				//create a temporary list of ParameterValueType objects from current seed object...
				ValuesType vts = (ValuesType)this._seed;
				List<string> vtList = new List<string>(vts.Value);

				//if the seed object contains the ParameterValue object's seed object, replace it with the new one...
				int idx = vtList.IndexOf(this[index].Value);
				{
					vtList[idx] = item.Value;
					vts.Value = vtList.ToArray();
				}
			}
			base.SetItem(index, item);
		}
		protected override void ClearItems()
		{
			if (!this._initializing)
			{
				this.PrepStateChange("DefaultValueListItem");

				ValuesType vts = (ValuesType)this._seed;
				vts.Value = null;
			}
			base.ClearItems();
		}

		protected override void OnAddingNew(System.ComponentModel.AddingNewEventArgs e)
		{
			//each ValuesType.Value[] string maps to a single DefaultValue object in order to support
			//notification, state management and other GenericBase<T> functionality...

			//append a new empty string value to the ValuesType.Value[] array
			ValuesType vts = (ValuesType)this._seed;
			List<string> values = new List<string>(vts.Value);
			values.Add(string.Empty);
			vts.Value = values.ToArray();

			//pass in the ValuesType seed object and the new index
			DefaultValue dv = new DefaultValue(vts, values.Count);
			e.NewObject = dv;

			base.OnAddingNew(e);
		}

		public override void CancelNew(int itemIndex)
		{
			ValuesType vts = (ValuesType)this._seed;

			List<string> values = new List<string>(vts.Value);
			values.RemoveAt(itemIndex);

			vts.Value = values.ToArray();

			base.CancelNew(itemIndex);
		}

		protected override void OnListChanged(System.ComponentModel.ListChangedEventArgs e)
		{
			if (!this._initializing && e.ListChangedType == ListChangedType.ItemChanged)
			{
				this.PrepStateChange("DefaultValueListItem");
				base.OnListChanged(e);
			}
		}

		public override CustomList<DefaultValue> CreateDeepCopy()
		{
			if (this._copy != null)
			{
				return this._copy;
			}
			return new DefaultValueList(this);
		}

		#endregion
	}
}
